package com.example.planetandzombiedemo.entity;

import android.graphics.Canvas;
import android.graphics.Paint;

import com.example.planetandzombiedemo.global.Config;
import com.example.planetandzombiedemo.model.BaseModel;
import com.example.planetandzombiedemo.model.Plantable;
import com.example.planetandzombiedemo.view.GameView;

import java.util.concurrent.Flow;

public class Flower extends BaseModel implements Plantable {
    //图片帧数组
    private int frameIndex = 0;
    //标记此处是否有植物
    private int mapIndex;
    //控制产生阳光的时间戳
    private long lastBirthTime;
    //摆动速度控制
    private boolean swingSpeed;

    public Flower(int locationX, int locationY, int mapIndex) {
        this.locationX = locationX;
        this.locationY = locationY;
        this.mapIndex = mapIndex;
        isAlife = true;
        lastBirthTime = System.currentTimeMillis();
        swingSpeed = false;
    }

    @Override
    public void drawSelf(Canvas canvas, Paint paint) {
        if (isAlife) {
          //通过SwingSpeed控制显示动画帧，动画帧每2帧显示
            canvas.drawBitmap(Config.flowerFrames[frameIndex],locationX,locationY,paint);
            if(!swingSpeed)
            {
                frameIndex=(++frameIndex)%8;
                swingSpeed=!swingSpeed;
            }
            else{
                swingSpeed=!swingSpeed;
            }
            //每10秒钟产生一个阳光
            if(System.currentTimeMillis()-lastBirthTime>10000)
            {
                lastBirthTime=System.currentTimeMillis();
                giveBirth2Sun();
            }
        }
    }

    @Override
    public int getModelWidth() {
        return Config.flowerFrames[0].getWidth();
    }

    @Override
    public int getMapIndex() {
        return mapIndex;
    }
    private void giveBirth2Sun()
    {
        GameView.getInstance().giveBirth2Sun(locationX,locationY);
    }
}
